#region Copyright 2006 - Evolution Tecnologia Ltda.
//
// Author: Rafael Franco Carvalho (rafael@Evolutiontec.com.br)
//
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: Group.cs
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace csharp.rad.general
{
    /// <summary>
    /// The User Group class
    /// </summary>
    public class Group
    {
        private String groupName;
        private String groupId;
        private String groupDescription;
        private Permissions groupPermissions;
        private Users groupUsers;
        private Database myDatabase;
        
        public Group(Database myDatabase)
        {
            this.myDatabase = myDatabase;
            groupPermissions = new Permissions(this.myDatabase);
            groupUsers = new Users(this.myDatabase);
        }

        public String Name
        {
            get
            {
                return groupName;
            }
            set
            {
                groupName = value;
            }
        }

        public String Id
        {
            get
            {
                return groupId;
            }
            set
            {
                groupId = value;
            }
        }

        public String Description
        {
            get
            {
                return groupDescription;
            }
            set
            {
                groupDescription = value;
            }
        }

        public Permissions Permissions
        {
            get
            {
                return groupPermissions;
            }
            set
            {
                groupPermissions = value;
            }
        }

        public Users Users
        {
            get
            {
                return groupUsers;
            }
            set
            {
                groupUsers = value;
            }
        }

        public void Clear()
        {
            groupName = null;
            groupId = null;
            groupDescription = null;
            groupPermissions.Clear();
            groupUsers.Clear();
        }

        /// <summary>
        /// Fill group data
        /// </summary>
        /// <param name="value">The GroupId</param>
        public void GetData(String value)
        {
            String commandText;
            String tableName = "groups";
            Database.Result databaseResult = new Database.Result();
            System.Data.DataSet sqlDataSet = new System.Data.DataSet();

            Clear();
            groupPermissions.Items.Clear();
            commandText = "SELECT group_id, "
                        + "       group_name, "
                        + "       group_description "
                        + "FROM groups "
                        + "WHERE group_id = '" + value + "'";
            sqlDataSet = myDatabase.ExecuteQuery(commandText, tableName, out databaseResult);
            foreach (System.Data.DataRow dataRecord in sqlDataSet.Tables[tableName].Rows)
            {
                groupId = System.Convert.ToString(dataRecord["group_id"]);
                groupName = System.Convert.ToString(dataRecord["group_name"]);
                groupDescription = System.Convert.ToString(dataRecord["group_description"]);
                //Get permissions of group
                groupPermissions.GetData(groupId);
            }
            sqlDataSet.Dispose();
        }

        public Boolean Exists()
        {
            String commandText;
            Database.Result databaseResult = new Database.Result();

            commandText = "SELECT group_id "
                        + "FROM groups "
                        + "WHERE group_id = '" + groupId + "'";
            return myDatabase.ExecuteScalar(commandText, out databaseResult) != null;
        }

        /// <summary>
        /// Insert or update group data
        /// </summary>
        /// <param name="value">The GroupId</param>
        /// <returns></returns>
        public Boolean SetData()
        {
            String commandText;
            Database.Result databaseResult = new Database.Result();

            //check for existing group
            if (Exists())
            {
                //update group data
                commandText = "UPDATE groups SET "
                            + "       group_name = '" + groupName + "', "
                            + "       group_description = '" + groupDescription + "' "
                            + "WHERE (group_id = '" + groupId + "');\r\n";
                //delete old permissions
                commandText = commandText
                            + "DELETE FROM permissions "
                            + "WHERE (permission_group_id = '" + groupId + "');\r\n";
            }
            else
            {
                groupId = System.Guid.NewGuid().ToString();
                commandText = "INSERT INTO groups ("
                            + "       group_id, "
                            + "       group_name, "
                            + "       group_description "
                            + ") VALUES ("
                            + "'" + groupId + "', "
                            + "'" + groupName + "', "
                            + "'" + groupDescription + "');\r\n";
            }
            //insert new permissions
            foreach (Permission permissionItem in groupPermissions.Items)
            {
                commandText = commandText
                            + "INSERT INTO permissions ("
                            + "       permission_id, "
                            + "       permission_action_id, "
                            + "       permission_group_id "
                            + ") VALUES ("
                            + "'" + System.Guid.NewGuid().ToString() + "', "
                            + "'" + permissionItem.ActionId + "', "
                            + "'" + groupId + "');\r\n";
            }
            databaseResult = myDatabase.ExecuteCommand(commandText, Database.doUseTransaction);
            if (databaseResult.Status == Database.Status.Successful)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Boolean DeleteData()
        {
            String commandText;
            Database.Result databaseResult = new Database.Result();

            commandText = "DELETE FROM groups "
                        + "WHERE (group_id = '" + groupId + "')";
            databaseResult = myDatabase.ExecuteCommand(commandText, Database.doUseTransaction);
            if (databaseResult.Status == Database.Status.Successful)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Get the action name of given actionId
        /// </summary>
        /// <param name="value">The actionId</param>
        /// <returns></returns>
        public String GetActionName(String value)
        {
            String result = "(desconhecido)";

            for (int i = 0; i < groupPermissions.Items.Count; i++)
            {
                if (groupPermissions.Items[i].ActionId == value)
                {
                    result = groupPermissions.Items[i].ActionName;
                    break;
                }
                else
                    result = "(desconhecido)";
            }
            return result;
        }
    }
}
